package vip.xiaonuo.biz.modular.watchtask.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.watchtask.entity.TaskLog;
import vip.xiaonuo.biz.modular.watchtask.mapper.TaskLogMapper;
import vip.xiaonuo.biz.modular.watchtask.param.TaskLogPageParam;
import vip.xiaonuo.biz.modular.watchtask.service.TaskLogService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.page.CommonPageRequest;

/**
 * 盯防任务日志表(TaskLog)表服务实现类
 *
 * @author h
 * @since 2025-11-20 17:00:41
 */
@Service("taskLogService")
public class TaskLogServiceImpl extends ServiceImpl<TaskLogMapper, TaskLog> implements TaskLogService {

    @Override
    public Page<TaskLog> page(TaskLogPageParam taskLogPageParam) {
        QueryWrapper<TaskLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TaskLog::getTaskId, taskLogPageParam.getTaskId());
        if(ObjectUtil.isAllNotEmpty(taskLogPageParam.getSortField(), taskLogPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(taskLogPageParam.getSortOrder());
            queryWrapper.orderBy(true, taskLogPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(taskLogPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(TaskLog::getCreateTime);
        }
        Page<TaskLog> page = this.baseMapper.selectPage(CommonPageRequest.defaultPage(), queryWrapper);
        return page;
    }
}
